package com.yf.service.impl;

import com.yf.entity.TokenEntity;
import com.yf.entity.WxUser;
import com.yf.enums.UserStatusEnum;
import com.yf.response.RRException;
import com.yf.service.TokenService;
import com.yf.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yf.dao.UserDao;
import com.yf.entity.UserEntity;
import com.yf.service.UserService;
import com.yf.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Dec 11 16:26:15 CST 2018
 */
@Service
@Slf4j
public class UserServiceImpl extends BaseServiceImpl<UserEntity> implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private TokenService tokenService;

    /**内部方法**/


    /**
     * 外部方法实现
     **/

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TokenEntity registerUser(WxUser wxUser, Long parentId) {
        //上级信息
        UserEntity parent = get(parentId);
        if (parent == null) {
            log.error("注册失败，parentId不存在:" + parentId);
            throw new RRException("推荐人不存在!ID:" + parentId);
        }
        //初始时间
        Date date = new Date();
        //邀请码


        UserEntity userEntity = new UserEntity(wxUser.getNickName(), wxUser.getHeadImgUrl(), wxUser.getSex(), wxUser.getOpenId(), wxUser.getUnionId(), parentId, date, date, UserStatusEnum.ACTIVE.getState());
        userDao.save(userEntity);
        //返回token
        return tokenService.createToken(userEntity.getId());
    }

    @Override
    @Transactional
    public TokenEntity validateLogin(UserEntity userEntity) {
        if(userEntity==null){
            throw new RRException("账户不存在!");
        }
        if(!userEntity.getStatus().equals(UserStatusEnum.ACTIVE.getState())){
            throw new RRException("用户没有激活!");
        }
        return tokenService.createToken(userEntity.getId());
    }

    @Override
    public UserEntity findByUnionId(String unionId) {
        List<UserEntity> list = userDao.findByUnionId(unionId);
        if(list==null||list.isEmpty()){
            return null;
        }
        if(list.size()>1){
            log.error("账号异常unionId不唯一:"+unionId);
            throw new RRException(unionId+"账号不唯一");
        }
        return list.get(0);
    }

    @Override
    public UserEntity login(String phone, String password) {
        UserEntity userEntity = userDao.login(phone,password);
        return userEntity;
    }


    //基本信息

    @Override
    public UserVO getInfo(Long userId) {
        return userDao.getInfo(userId);
    }

    @Override
    public void saveInfo(Long userId, String sex, String phone, String realName, String idNumber, Date birthday, String address) {
        UserEntity userEntity = new UserEntity(userId,sex, phone, realName, idNumber, birthday, address);
        userDao.update(userEntity);
    }
}
